package com.buyinglist.app.web;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.buyinglist.app.facade.GeoFacade;
import com.buyinglist.app.facade.UserFacade;
import com.buyinglist.app.view.CityView;
import com.buyinglist.app.view.CountryView;
import com.buyinglist.app.view.RegionView;
import com.buyinglist.app.view.UserView;


@Controller
@RequestMapping("ajax")
public class Ajax {

	@Autowired
	private GeoFacade geoFacade;
	
	@Autowired
	private UserFacade userFacade;
	
	private static final Logger logger = LoggerFactory.getLogger(Ajax.class);
	

	@RequestMapping(value="/country", method=RequestMethod.GET)
	public ModelAndView ajaxcountrylist (HttpServletRequest request){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String country=request.getParameter("term");
		if (country==null)
			return null;
		List <CountryView> l=geoFacade.findCountrybyprefix(country);
		String sendData=String.format("[\"%1$s\"", l.get(0).getName());
		for (int i=1;i<l.size();i++)
			sendData+=String.format(",\"%1$s\"", l.get(i).getName());
		sendData+="]";
		logger.info("List of country prepared");
		ModelAndView m =new ModelAndView("ajax/ajaxresp");
		m.addObject("sendData", sendData);
		return m;
	}

	@RequestMapping(value="/region", method=RequestMethod.GET)
	public ModelAndView ajaxregionlist (HttpServletRequest request){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String region=request.getParameter("term");
		if (region==null)
			return null;
		List <RegionView> l=geoFacade.findRegionbyprefix(region);
		String sendData=String.format("[{\"label\":\"%1$s\",\"value\":\"%2$s\",\"country\":\"%3$s\"}", l.get(0).getName()+";"+l.get(0).getCountry().getName(),l.get(0).getName(),l.get(0).getCountry().getName());
		for (int i=1;i<l.size();i++){
			sendData+=String.format(",{\"label\":\"%1$s\",\"value\":\"%2$s\",\"country\":\"%3$s\"}", l.get(i).getName()+";"+l.get(i).getCountry().getName(),l.get(i).getName(),l.get(i).getCountry().getName());
		}
		sendData+="]";
		logger.info("List of city prepared");
		ModelAndView m =new ModelAndView("ajax/ajaxresp");
		m.addObject("sendData", sendData);
		return m;
	}

	@RequestMapping(value="/city", method=RequestMethod.GET)
	public ModelAndView ajaxcitylist (HttpServletRequest request){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String city=request.getParameter("term");
		if (city==null)
			return null;
		List <CityView> l=geoFacade.findCitybyprefix(city);
		String sendData=String.format("[{\"label\":\"%1$s\",\"value\":\"%2$s\",\"region\":\"%3$s\",\"country\":\"%4$s\"}", l.get(0).getName()+"; "+l.get(0).getRegion().getName()+"; "+l.get(0).getRegion().getCounty().getName(),l.get(0).getName(),l.get(0).getRegion().getName(),l.get(0).getRegion().getCounty().getName());
		for (int i=1;i<l.size();i++){
			sendData+=String.format(",{\"label\":\"%1$s\",\"value\":\"%2$s\",\"region\":\"%3$s\",\"country\":\"%4$s\"}", l.get(i).getName()+"; "+l.get(i).getRegion().getName()+"; "+l.get(i).getRegion().getCounty().getName(),l.get(i).getName(),l.get(i).getRegion().getName(),l.get(i).getRegion().getCounty().getName());
		}
		sendData+="]";
		logger.info("List of city prepared");
		ModelAndView m =new ModelAndView("ajax/ajaxresp");
		m.addObject("sendData", sendData);
		return m;
	}
	
	@RequestMapping(value="/loginvalidation", method=RequestMethod.GET)
	public ModelAndView loginvalid(HttpServletRequest request){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String login=request.getParameter("term");
		if (login==null)
			return null;
		List <UserView> us=userFacade.findUserIdLogin(login);
		Boolean valid=us.size()==0;
		logger.info("login has validated.");
		ModelAndView m=new ModelAndView("ajax/validationregistry");
		m.addObject("result", valid);
		m.addObject("field", "login");
		return m;
	}

	
}
